package com.auxgroup.hr.front.site.co.hireperson;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.dao.domain.PsCHireExpWork;
import com.auxgroup.hr.front.dao.domain.PsCHirePrimaryClass;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.hireperson.HireExpWorkService;
import com.auxgroup.hr.front.site.co.BaseCO;

/**
 * 以前工作经历CO
 * @author lvliuhao
 * @version 创建时间：180526 
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/hireExpWork")
public class HireExpWorkCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(HireExpWorkCO.class);

	@Autowired
	public HireExpWorkService hireExpWorkService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;

	/**
	 * 获取以前工作经历
	 * @param hireId
	 * @return
	 */
	@GetMapping(value = "/getOne")
	public BaseResponseVO getOne(String rsHireId,BigDecimal cSeqno) {
		logger.info("getHireExpWork param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(cSeqno==null){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"语言的seqno不能为空");
		}
		PsCHirePrimaryClass hirePrimary = new PsCHirePrimaryClass(cSeqno, rsHireId);
		PsCHireExpWork hireExpWork = hireExpWorkService.getHireExpWork(hirePrimary);
		return buildRespone(hireExpWork);
	}
	
	/**
	 * 保存以前工作经历
	 * @param hireId
	 * @return
	 */
	@PostMapping(value = "/save")
	public BaseResponseVO save(PsCHireExpWork hireExpWork) {
		logger.info("saveHireExpWork param= {}", hireExpWork);
		if(StringUtils.isBlank(hireExpWork.getRsHireId())){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(hireExpWork.getcSeqno()!=null){
			PsCHirePrimaryClass primary = new PsCHirePrimaryClass();
			primary.setcSeqno(hireExpWork.getcSeqno());
			primary.setRsHireId(hireExpWork.getRsHireId());
			PsCHireExpWork nowExpWork = hireExpWorkService.getHireExpWork(primary);
			if(nowExpWork==null){
				hireExpWork = hireExpWorkService.save(hireExpWork);
			}else{
				objectMapper.map(hireExpWork, nowExpWork,"notCopyNull_hireExpWork");
				hireExpWork = hireExpWorkService.save(nowExpWork);
			}
		}else{
			hireExpWork = hireExpWorkService.save(hireExpWork);
		}
		if(hireExpWork.getcSeqno()!=null){
			return buildRespone(true);
		}
		return buildRespone(false);
		
	}
	
	/**
	 * 删除以前工作经历
	 * @param hireId
	 * @return
	 */
	@PostMapping(value = "/del")
	public BaseResponseVO del(String rsHireId,BigDecimal cSeqno) {
		logger.info("delHireExpWork param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(cSeqno==null){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"语言的seqno不能为空");
		}
		PsCHirePrimaryClass hirePrimary = new PsCHirePrimaryClass(cSeqno, rsHireId);
		Boolean isdel = hireExpWorkService.delete(hirePrimary);
		return buildRespone(isdel);
	}
	
	/**
	 * 获取全部以前工作经历
	 * @param rsHireId
	 * @return
	 */
	@GetMapping(value = "/getAll")
	public BaseResponseVO getAll(String rsHireId){
		logger.info("getHireExpWork param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		List<PsCHireExpWork> list = hireExpWorkService.getAll(rsHireId);
		return buildRespone(list);
	}
	
	

}
